#ifndef SCHEMATICBASE_H
#define SCHEMATICBASE_H

#include <QWidget>
#include <QTreeWidgetItem>

#include "funcblockbase.h"
#include "wirebase.h"
#include "pinbase.h"

class FuncBlockBase;
class WireBase;
class PinBase;

enum editMode_t
{
	emWire, // neue Wire zeichnen
	emKink, // Wire knicken
};

class SchematicBase : public QWidget
{
    Q_OBJECT
public:
    explicit SchematicBase(QWidget *parent = 0);
    ~SchematicBase();
    void registerFuncBlock(FuncBlockBase *fb);
	void unregisterFuncBlock(FuncBlockBase *fb);
    void registerWire(WireBase *wire);
    void unregisterWire(WireBase *wire);
    WireBase* startNewWire(QObject *parent, PinBase *pin, int x, int y);
    WireBase* endNewWire(int x, int y);
    unsigned int getNewConnectID();
    void New();
    bool SaveAs(QString filename);
    bool Open(QString filename);
    void setSelectedFuncBlock(FuncBlockBase *fb);
    void roundToGridPos(QPoint *pos);
    FuncBlockBase* getSelectedFuncBlock() {return selectedFuncBlock;};
    PinBase* getNewPin(int x, int y, unsigned char datawidth);
    void setEditMode(enum editMode_t em) {editMode = em;};
    QList<FuncBlockBase*>* getfbListRef() {return fbList;};
    QList<WireBase*>* getwireListRef() {return wireList;};
    QList<PinBase*>* getpinListRef() {return pinList;};

private:
    QList<FuncBlockBase*> *fbList;
    QList<WireBase*> *wireList;
    QList<PinBase*> *pinList;
    WireBase *moveWire;
    PinBase *movePin;
    QPoint offset;
	bool bDockMode;
	bool bWirePaintMode;
    bool bPinPaintMode;
    unsigned int connectid;
    void paintEvent(QPaintEvent *);
    WireBase* checkWire(QPoint *pos, PinBase *excludePin);
    PinBase* checkPin(QPoint *pos, PinBase *excludePin, unsigned char datawidth);
    void splitWire(WireBase *wire, QPoint pos, PinBase *newPin);
    WireBase *selectedWire;
    PinBase *selectedPin; // nur in Schematic, kein Pin innerhalb eines FBs
    FuncBlockBase *selectedFuncBlock;
    QPoint mouseStartPos;
    enum editMode_t editMode;
    PinBase* findPinById(unsigned int pinid);

protected:
    virtual void mousePressEvent(QMouseEvent* me);
    virtual void mouseMoveEvent(QMouseEvent* me);
    virtual void mouseReleaseEvent(QMouseEvent* me);
    virtual void keyPressEvent(QKeyEvent *ke);

signals:
	void onFuncBlockSelectChanged(FuncBlockBase *fb);

public slots:
    void addFunctionBlock(QTreeWidgetItem *twi, int col);
};

#endif // SCHEMATICBASE_H
